package com.runmi.webchat.aftersale.common.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;


public class StringUtil {
	
	 public static boolean isPicture(String  pInput,String pImgeFlag){
		// 文件名称为空的场合
		if(isBlank(pInput)){
			// 返回不和合法
			return false;
		}
		// 获得文件后缀名
		String tmpName = pInput.substring(pInput.lastIndexOf(".") + 1,
		                pInput.length());
		// 声明图片后缀名数组
		String imgeArray [][] = {
		{"bmp", "0"}, {"dib", "1"}, {"gif", "2"},
		{"jfif", "3"}, {"jpe", "4"}, {"jpeg", "5"},
		{"jpg", "6"}, {"png", "7"} ,{"tif", "8"},
		{"tiff", "9"}, {"ico", "10"}
		};
		// 遍历名称数组
		for(int i = 0; i<imgeArray.length;i++){
			// 判断单个类型文件的场合
			if(!isBlank(pImgeFlag)&& imgeArray [i][0].equals(tmpName.toLowerCase())&& imgeArray [i][1].equals(pImgeFlag)){
				return true;
			}
			// 判断符合全部类型的场合
			if(isBlank(pImgeFlag)&& imgeArray [i][0].equals(tmpName.toLowerCase())){
				return true;
				}
		}
		return false;
	}

	public static String getUUID(){ 
		String uuid = UUID.randomUUID().toString(); 
		//去掉“-”符号 
		return uuid.replaceAll("-", "");
	}
	
	public static boolean isNull(String arg) {
		return arg == null ? true : false;
	}

	public static boolean isEmpty(String arg) {
		return "".equals(arg) ? true : false;
	}

	public static boolean isNullStr(String arg) {
		return "null".equals(arg) ? true : false;
	}

	/**
	 * 检查字符串不是null, "", "null"
	 * 
	 * @param arg
	 * @return true/非空, false/验证不通过
	 */
	public static boolean isNotEmptyStr(String arg) {
		return !(isNull(arg) || isEmpty(arg) || isNullStr(arg)) ? true : false;
	}

	/**
	 * 截取字符串后面部分字符.
	 * 
	 * @param str
	 * @param length
	 *            表示双字节长度,比如4,表示返回的字符串长度为8字节.
	 * @param addPoints
	 * @return
	 */
	public static String trimWords(String str, int length, boolean addPoints) {
		String wordStr = str;
		if (wordStr == null || wordStr.equals("")) {
			return "";
		}
		int byteLen = length * 2;
		byte[] strBytes = wordStr.getBytes();
		if (strBytes.length == str.length()) {
			if (strBytes.length <= byteLen) {
				return wordStr;
			}
			byte[] trimBytes = new byte[byteLen];
			System.arraycopy(strBytes, 0, trimBytes, 0, byteLen);
			wordStr = new String(trimBytes);
		} else {
			if (wordStr.length() <= length) {
				return str;
			}
			wordStr = left(str, length);
		}
		if (addPoints) {
			wordStr += "...";
		}
		return wordStr;
	}

	/**
	 * 从左起取字符串前n位。
	 * 
	 * @param str
	 * @param length
	 * @return
	 */
	public static String left(String str, int length) {
		if (str == null) {
			throw new IllegalArgumentException("字符串参数值不能为null");
		}
		if (length < 0) {
			throw new IllegalArgumentException("整型参数长度不能小于0");
		}
		if (str.length() < length) {
			throw new IllegalArgumentException("字符串参数长度不能小于" + length);
		}

		return str.substring(0, length);
	}

	/**
	 * 过滤html代码
	 * 
	 * @param content
	 * @return
	 */
	public static String htmlContentConvert(String content) {
		content = content.replace("<", "&lt;");
		content = content.replace(">", "&gt;");
		content = content.replace("&", "&amp;");
		content = content.replace("\"", "&quot;");
		content = content.replace("'", "&apos;");
		content = content.replace("'", "&quot;");
		return content;
	}

	/**
	 * 将数据库表字段字符串转换为驼峰标识字符串
	 * 
	 * @param fieldName
	 * @return
	 */
	public static String convertField(String fieldName) {
		// check参数
		if (!isNotEmptyStr(fieldName)) {
			return null;
		}

		// buffer
		StringBuffer tmpData = new StringBuffer();
		// 转换为小写, 分割字符串
		fieldName = fieldName.toLowerCase();
		String[] tmpDatas = fieldName.split("_");
		if ((null == tmpDatas) || (0 == tmpDatas.length)) {
			return null;
		}		
		int length = tmpDatas.length;
		if (1 == length) {
			tmpData.append(tmpDatas[0]);
			return tmpData.toString();
		} else {
			String data = "";
			tmpData.append(tmpDatas[0]);
			for (int index = 1; index < length; index++) {
				data = tmpDatas[index];
				// 将第一个字母转换为大写
				if(StringUtil.isNotEmptyStr(data)){
					if(data.length()>1){
						data=data.substring(0,1).toUpperCase() + data.substring(1);	
					}else{
						data=data.substring(0,1).toUpperCase();
					}
				}								
				tmpData.append(data);
			}
		}

		return tmpData.toString();
	}
	
	/**
	 * 将字符串的第index字符转换为大写
	 * 
	 * @param data
	 * @param index
	 * @return
	 */
	public static String convertString(String data, int index) {
		// check
		if (StringUtil.isNull(data) || StringUtil.isEmpty(data)) {
			return data;
		}
		
		// 字符串长度
		int length = data.length();
		if ((index < 0) || (length < index)) {
			return data;
		}
		
		// 取出第index字母并进行替换
		char beginOld = data.charAt(0);
		char beginNew = (beginOld + "").toUpperCase()
				.charAt(0);
		data = data.replace(beginOld, beginNew);
				
		return data;
	}
	
	/**
	 * 判断一个字符串是否是数字
	 * 
	 * @param number
	 * @return true/是数字, false/不是数字
	 */
	public static boolean isNumeric(String number) {
		String model = "[0-9]*";
		
		Pattern pattern = Pattern.compile(model);
		Matcher isNum = pattern.matcher(number);
		
		return isNum.matches();
	}
	
	
	/**
	 * 判断一个字符串是否是汉字
	 * 
	 * @param number
	 * @return true/是, false/不是
	 */
	public static boolean isHanzi(String str) {
		 boolean temp = false;
	       Pattern p=Pattern.compile("[\u4e00-\u9fa5]"); 
	       Matcher m=p.matcher(str); 
	       if(m.find()){ 
	           temp =  true;
	       }
	       return temp;
	}
	/**
	 * 判断一个字符串是否是字母
	 * 
	 * @param number
	 * @return true/是, false/不是
	 */
	public static boolean isZimu(String fstrData){   
		char c = fstrData.charAt(0);   
		if(((c >= 'a'&& c <= 'z') || (c >= 'A'&& c <= 'Z'))){   
			return true;   
		}else{   
			return false;   
		}   
	}
	
	/**
	 * 判断一个字符串是否是整数数字，包括负数
	 * @param number
	 * @return
	 */
	public static boolean isInteger(String number) {
		String model = "-?[0-9]*";
		
		Pattern pattern = Pattern.compile(model);
		Matcher isNum = pattern.matcher(number);
		
		return isNum.matches();
	}
	
	/**
	 * 判断字符串是否为 "" 或 null
	 * @param str
	 * @return boolean true表示为 "" 或 null,false表示不为"" 或  null
	 */
	public static boolean isBlank(String str){
		return isNull(str) || str.trim().length() == 0 || str.trim().equals("null");
	}
	
	/**
	 * 判断字符串是否是有效的商品编码
	 * @param upc
	 * @return
	 */
	public static boolean isValidUpc(String upc) {
		if(null != upc){
			upc = upc.trim();
			if(upc.length() == 13 || upc.length() == 8){
				if(upc.length() == 8){upc = "00000" + upc;}
				String code = upc.substring(0,12);
				int sumj = 0, sume = 0;
				int result = 0;
				for (int i = 0; i < code.length() - 1; i = i + 2) {
					sumj += code.charAt(i) - '0';
					sume += code.charAt(i + 1) - '0';
				}
				result = sumj + sume * 3;
				result = result % 10;
				result = 10 - result;
				if(result == 10){
					result = 0;
				}
				String calUpc = code + result;
				return calUpc.equals(upc);
			}
		}
		return false;
	}
	
	/**
	 * 获取字符串的非null格式,字符串为空则返回""
	 * @param str String
	 * @return
	 */
	public static String getStr(String str){
		if(null == str){
			return "";
		}
		return str;
	}
	
	/**
	 * 判断多个字符串中是否有一个字符串为空
	 * @param strs
	 * @return boolean 有一个字符串为空则或数组为空则返回true,反之,返回false
	 */
	public static boolean hasBlankStr(String... strs){
		if(null != strs){ 
			for(String str : strs){
				if(isBlank(str)){
					return true;
				}
			}
		}else{
			return true;
		}
		return false;
	}
	
	/**
	 * 随机生成销售流水单号
	 * @return
	 */
	public static String generateRandId(){
		String dateStr = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		Random rd = new Random();
		String n="";
		int getNum;
		do {
			 getNum = Math.abs(rd.nextInt())%10 + 48;//产生数字0-9的随机数
			 //getNum = Math.abs(rd.nextInt())%26 + 97;//产生字母a-z的随机数
			 char num1 = (char)getNum;
			 String dn = Character.toString(num1);
			 n += dn;
		} while (n.length()<6);
		return dateStr+n;
	}
	
	/**
	 * 随机生成入库流水单号
	 * @return
	 */
	public static String generatePurchaseRandId(){
		Random rnd = new Random();
		StringBuilder sb = new StringBuilder(10);
		for(int i=0; i < 10; i++) {
			sb.append('0' + rnd.nextInt(10));
		}
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ":" + sb.toString();
	}
	
	/**
	 * 设置对象的字符串的默认值为""
	 * @param str
	 * @return 若str为null,返回"",反之返回str
	 */
	public static String defaultEmptyStr(Object obj){
		return defaultStr(obj,null);
	}
	
	/**
	 * 设置对象的字符串的默认值
	 * @param str 
	 * @param defaultStr 
	 * @return  str为null,defaultStr不为null,返回defaultStr,反之返回"";否则返回str
	 */
	public static String defaultStr(Object obj,String defaultStr){
		if(null == obj){
			if(null != defaultStr){
				return defaultStr;
			}else{
				return "";
			}
		}else{
			return obj.toString();
		}
	}
	
	/**
	 * 获取远程地址
	 * @param request
	 * @return
	 */
	public static String getRemoteAddress(HttpServletRequest request) {  
        String ip = request.getHeader("x-forwarded-for");  
        if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
            ip = request.getHeader("Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
            ip = request.getHeader("WL-Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
            ip = request.getRemoteAddr();  
        }  
        return ip;  
    } 

	/**
	 * 是否手机号码
	 * @param mobiles
	 * @return
	 */
	public static boolean isMobileNum(String mobiles) {
        if (isBlank(mobiles)) {
            return false;
        }
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,1,5-9])|(17[6，7,8]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();

    }
	
	/**
	 * 验证邮箱
	 *
	 * @param email
	 * @return
	 */

	public static boolean checkEmail(String email) {
	    boolean flag = false;
	    try {
	        String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
	        Pattern regex = Pattern.compile(check);
	        Matcher matcher = regex.matcher(email);
	        flag = matcher.matches();
	    } catch (Exception e) {
	        flag = false;
	    }
	    return flag;
	}

	public static void main(String[] args) {
		//System.out.println(isValidCardNum("8001103532070"));
//		String prefixStr = "45, 47, 35, 23, 36, 33, 34, 13, 00, 14, 37, 12, 21, 43, 42, 41, 51, 52, 62, 61, 50";
//		List<String> prefixList = Arrays.asList(prefixStr.split(","));
//		System.out.println(prefixList.contains(new String("00")));
	}
	
}
